//
//  QuizHeadView.m
//  SubmitVCDemo
//
//  Created by Tianlong on 2018/1/25.
//  Copyright © 2018年 Tianlong. All rights reserved.
//

#import "QuizHeadView.h"
#import "QuizSubmitTopView.h"
#import "QuizScoreView.h"
#import "QuizTalkView.h"

#define LeftM 30

@interface QuizHeadView ()<QuizAnalyViewDelegate>
@property (assign, nonatomic) QuizSubmitType type;
// 顶部提交结果
@property (weak, nonatomic) QuizSubmitTopView *topV;
// 解析
@property (weak, nonatomic) QuizAnalyView *analyV;
// 分数
@property (weak, nonatomic) QuizScoreView *scoreV;
// 有话说
@property (weak, nonatomic) QuizTalkView *talkV;
// 排行榜头部
@property (weak, nonatomic) UIView *listHead;
// 整体高度
@property (assign, nonatomic) CGFloat realH;
@end

@implementation QuizHeadView
+ (instancetype)headViewWithSubmitType:(QuizSubmitType)type{
    QuizHeadView *head = [[QuizHeadView alloc] initWithSubmitType:type];
    return head;
}

- (instancetype)initWithSubmitType:(QuizSubmitType)type
{
    self = [super init];
    
    if (self) {
        self.type = type;
        switch (self.type) {
            
            case QuizSubmitTypeReject:
                //直接驳回: 顶部
                [self quizSubmitTypeReject];
                break;
            
            case QuizSubmitTypeAllCorrect:
                //无错误: 分数+解析+排行榜+有话说
                [self quizSubmitTypeAllCorrect];
                break;
                
            case QuizSubmitTypeCorrectReject:
                //订正驳回: 顶部+解析+(排行榜)
                [self quizSubmitTypeCorrectReject];
                
                //排行榜头部
                [self addListTitleView];
                
                break;
            
            case QuizSubmitTypeSubmitSuccess:
                //提交成功: 顶部+解析
                [self quizSubmitTypeSubmitSuccess];
                break;
            
            case QuizSubmitTypeModifiMistake:
                //无错误: 分数+解析+排行榜+有话说
                [self quizSubmitTypeModifiMistake];
                
                //排行榜头部
                [self addListTitleView];
                break;
                
            default:
                break;
        }
    }
    return self;
}

// 添加班级排名列表的头部视图
- (void)addListTitleView{
    CGFloat labelH = 16;
    CGFloat listTopM = 40;
    CGFloat listBottomM = 10;
    CGFloat listHeadH = listTopM+labelH+listBottomM;
    
    UIView *listHead = [[UIView alloc] init];
    [self addSubview:listHead];
    __weak typeof(self)ws = self;
    [listHead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.analyV.mas_bottom).offset(0);
        make.left.offset(LeftM);
        make.right.offset(-LeftM);
        make.height.offset(listHeadH);
    }];
    self.listHead = listHead;
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont fontPingFangSCWithSize:16];
    label.textColor = [UIColor colorWithHexValue:0x666666];
    label.text = @"班级排名";
    [listHead addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.listHead.mas_left).offset(26);
        make.bottom.equalTo(ws.listHead.mas_bottom).offset(-listBottomM);
        make.height.offset(labelH);
    }];
    
    UIImageView *imgV = [[UIImageView alloc] init];
    imgV.image = [UIImage imageNamed:@"homeworkpapertest_task_RankingList_icon_normal"];
    [listHead addSubview:imgV];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.listHead.mas_left).offset(0);
        make.centerY.equalTo(label.mas_centerY);
        make.width.offset(13);
        make.height.offset(13);
    }];
    
    self.realH += listHeadH;
}

#pragma mark - 创建视图
- (void)addTop{
    if(!self.topV){
        QuizSubmitTopView *topV = [QuizSubmitTopView viewType:self.type];
        self.topV = topV;
        [self addSubview:topV];
    }
}
- (void)addAnalyV{
    if(!self.analyV){
        QuizAnalyView *analy = [QuizAnalyView viewWithDataSource:nil];
        analy.delegate = self;
        self.analyV = analy;
        [self addSubview:analy];
    }
}

- (void)addScoreV{
    if (!self.scoreV) {
        QuizScoreView *scoreV = [QuizScoreView scoreView];
        self.scoreV = scoreV;
        [self addSubview:scoreV];
    }
}

- (void)addTalkV{
    if(!self.talkV){
        QuizTalkView *talkV = [QuizTalkView talkView];
        self.talkV = talkV;
        [self addSubview:talkV];
    }
}

#pragma mark - 真实高度
- (CGFloat)getRealHeight{
    return self.realH;
}

#pragma mark - QuizAnalyViewDelegate
- (void)analyView:(QuizAnalyView *)analyView didSelectItem:(QuizAnalyListItem *)item atIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(analyView:didSelectItem:atIndexPath:)]){
        [self.delegate analyView:analyView didSelectItem:item atIndexPath:indexPath];
    }
}

#pragma mark - 直接驳回
-(void)quizSubmitTypeReject{
    __weak typeof(self)ws = self;
    [self addTop];
    CGFloat topH = [self.topV getRealHeight];
    CGFloat topM = [QuizSubmitTopView topMargin];
    [self.topV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.centerY.equalTo(ws.mas_centerY).offset(-topM);
        make.left.right.offset(0);
        make.height.offset(topH);
    }];
    self.realH+=[UIScreen mainScreen].bounds.size.height-84-64;
}

#pragma mark - 无错误 分数+解析+排行榜+有话说
- (void)quizSubmitTypeAllCorrect{
    __weak typeof(self)ws = self;
    [self addScoreV];
    CGFloat scoreH = [QuizScoreView getRealHeight];
    [self.scoreV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.equalTo(ws.mas_top);
        make.left.right.offset(0);
        make.height.offset(scoreH);
    }];
    self.realH += scoreH;
    
    [self addTalkV];
    CGFloat talkH = [QuizTalkView getRealHeight];
    [self.talkV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.scoreV.mas_centerX);
        make.top.equalTo(ws.scoreV.mas_bottom).offset(0);
        make.left.right.offset(0);
        make.height.offset(talkH);
    }];
    self.realH += talkH;
    
    [self addAnalyV];
    CGFloat analyH = [QuizAnalyView getRealHeightWithDataSourceCount:TestCount];
    [self.analyV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.equalTo(ws.talkV.mas_bottom).offset(0);
        make.left.right.offset(0);
        make.height.offset(analyH);
    }];
    self.realH += analyH;
}

#pragma mark - 订正驳回 视图:顶部+解析+(排行榜)
- (void)quizSubmitTypeCorrectReject{
    __weak typeof(self)ws = self;
    [self addTop];
    CGFloat topH = [self.topV getRealHeight];
    [self.topV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(topH);
    }];
    self.realH+=topH;
    
    [self addAnalyV];
    CGFloat analyToTopM = 40;
    CGFloat analyY = [QuizAnalyView getRealHeightWithDataSourceCount:TestCount];
    [self.analyV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.equalTo(ws.topV.mas_bottom).offset(analyToTopM);
        make.left.right.offset(0);
        make.height.offset(analyY);
    }];
    self.realH+=analyY;
    self.realH+=analyToTopM;
}

#pragma mark - 提交成功 视图:顶部+解析
- (void)quizSubmitTypeSubmitSuccess{
    __weak typeof(self)ws = self;
    [self addTop];
    CGFloat topH = [self.topV getRealHeight];
    [self.topV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(topH);
    }];
    self.realH += topH;
    
    [self addAnalyV];
    CGFloat analyH = [QuizAnalyView getRealHeightWithDataSourceCount:TestCount];
    CGFloat analyTOTopM = 40;
    [self.analyV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.equalTo(ws.topV.mas_bottom).offset(analyTOTopM);
        make.left.right.offset(0);
        make.height.offset(analyH);
    }];
    self.realH += analyH;
    self.realH += analyTOTopM;
}

#pragma mark - 修改错题 分数+解析+有话说
- (void)quizSubmitTypeModifiMistake{
    __weak typeof(self)ws = self;
    [self addScoreV];
    CGFloat scoreH = [QuizScoreView getRealHeight];
    [self.scoreV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.equalTo(ws.mas_top);
        make.left.right.offset(0);
        make.height.offset(scoreH);
    }];
    self.realH += scoreH;
    
    [self addTalkV];
    CGFloat talkH = [QuizTalkView getRealHeight];
    [self.talkV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.scoreV.mas_centerX);
        make.top.equalTo(ws.scoreV.mas_bottom).offset(0);
        make.left.right.offset(0);
        make.height.offset(talkH);
    }];
    self.realH += talkH;
    
    [self addAnalyV];
    CGFloat analyH = [QuizAnalyView getRealHeightWithDataSourceCount:TestCount];
    [self.analyV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.top.equalTo(ws.talkV.mas_bottom).offset(0);
        make.left.right.offset(0);
        make.height.offset(analyH);
    }];
    self.realH += analyH;
}

@end
